package v1

import (
	"gomicro-sharemod/gateway-api/controllers"
	"gomicro-sharemod/gateway-api/tools"
	"strconv"

	pbRegion "gomicro-sharemod/services/common-service/proto/region"

	"github.com/gin-gonic/gin"
)

/*
 +----------------------------------------------------------------------
 + Title        : RegionController
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2019-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 区域控制器
 +----------------------------------------------------------------------
*/
type RegionController struct {
	controllers.BaseController
}

/**
 * 获取地区信息
 *
 * @param int id --------------------------------------------非必传,区域Id
 * @param string pid ----------------------------------------非必传,父级Id
 * @param string name ---------------------------------------非必传,名称
 * @param int level -----------------------------------------非必传,级别
 * @param string isHost -------------------------------------非必传,是否热门
 * @return Json
 * @author huwl
 */
func (control RegionController) GetRegions(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultQuery("id", "0"), 10, 64)
	pid := ctx.DefaultQuery("pid", "all")
	name := ctx.DefaultQuery("name", "")
	level, _ := strconv.ParseInt(ctx.DefaultQuery("level", "0"), 10, 64)
	isHot := ctx.DefaultQuery("is_hot", "all")

	// grpc远程调用公共微服务
	commonServiceClient := pbRegion.NewRegionService(tools.CommonServiceName, tools.MicroServer.Client())
	result, err := commonServiceClient.RegionList(ctx, &pbRegion.QueryRegionRequest{
		Id:       uint32(id),
		Pid:      pid,
		Name:     name,
		Level:    int32(level),
		IsHot:    isHot,
		SortType: "asc",
		Page:     1,
		PageSize: -1,
	})
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"regions": result.Regions,
	}))
}
